import 'rxjs/add/operator/toPromise';
import { Injectable } from '@angular/core';
import { Api } from './api.service';
import {MatSnackBar} from '@angular/material';
import {SnackSuccessComponent} from '../page/snack-success/snack-success.component';
import {SnackErrorComponent} from '../page/snack-error/snack-error.component';
import {SnackService} from './snack.service';
import {Router} from '@angular/router';

@Injectable()
export  class RegisterService {
  constructor(
    public api: Api,
    public snackBar: MatSnackBar,
    public snackService: SnackService,
    public router: Router
  ) {}

  addCompany(companyInfo: any) {
    const seq = this.api.post2('addCompany', companyInfo);
     // debugger
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '注册成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }

  addTalent(talentInfo: any) {
    const seq = this.api.post2('addTalent', talentInfo);
    seq.subscribe((res: any) => {
      if (res == 'OK') {
        this.snackService.message = '注册成功';
        this.snackBar.openFromComponent(SnackSuccessComponent, {
          duration: 3000,
        });
        this.returnLogin();
      } else {
      }
    }, err => {
      console.error('ERROR', err);
      this.snackService.message = err.error.message;
      this.snackBar.openFromComponent(SnackErrorComponent, {
        duration: 3000,
      });
    });

    return seq;
  }

  returnLogin() {
    this.router.navigate(['/login']);
  }
}
